Skip to content

TypeScript tsconfig.json 配置详解

一、核心要点速览

💡 核心考点

  • tsconfig.json 作用: 指定根文件、编译选项、包含/排除规则
  • 关键配置项: target, module, strict, esModuleInterop, declaration
  • 严格模式: strict 系列选项对类型安全的影响
  • 模块解析: moduleResolution 在现代项目中的应用

二、tsconfig.json 基础结构

1. 标准配置文件

json
{
  "compilerOptions": {
    // 目标 JavaScript 版本
    "target": "ES2020",
    
    // 模块系统
    "module": "ESNext",
    
    // 生成声明文件
    "declaration": true,
    
    // 严格类型检查
    "strict": true,
    
    // ES 模块互操作
    "esModuleInterop": true,
    
    // 跳过库文件检查
    "skipLibCheck": true,
    
    // 强制一致的文件大小写
    "forceConsistentCasingInFileNames": true
  },
  
  // 包含的文件
  "include": [
    "src/**/*"
  ],
  
  // 排除的文件
  "exclude": [
    "node_modules",
    "dist"
  ]
}

三、核心编译选项详解

1. JavaScript 目标版本 (target)

json
{
  "compilerOptions": {
    "target": "ES2020"
  }
}

可选值对比:

选项说明适用场景
ES3ES3 语法 (已过时)老旧浏览器
ES5ES5 语法需要兼容 IE11
ES2015/ES6ES2015 语法现代浏览器
ES2020ES2020 语法推荐 ✅
ESNext最新 ES 特性前沿项目

代码示例:

typescript
// 当 target: "ES5" 时,可选链会被转换
const name = obj?.user?.name;

// 编译后:
var _a, _b;
const name = (_b = (_a = obj.user) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : undefined;

2. 模块系统 (module)

json
{
  "compilerOptions": {
    "module": "ESNext"
  }
}

可选值:

选项输出格式使用场景
CommonJSrequire/exportsNode.js 项目
ES6/ES2015import/export现代浏览器
ESNext最新模块特性Vite/Webpack 打包 ✅
AMDdefine/requireRequireJS
UMD通用模块库开发

3. 严格类型检查 (strict)

json
{
  "compilerOptions": {
    "strict": true
  }
}

strict 包含的子选项:

选项说明推荐
strictNullChecks严格 null 检查✅ 必开
strictFunctionTypes严格函数类型检查✅ 必开
strictBindCallApply严格 bind/call/apply✅ 必开
strictPropertyInitialization类属性初始化检查✅ 必开
noImplicitThis隐式 this 检查✅ 必开
alwaysStrict使用严格模式✅ 必开

代码示例:

typescript
// strictNullChecks: false (不安全)
let name: string = null; // ❌ 允许但危险

// strictNullChecks: true (安全)
let name: string = null; // ✅ 报错:Type 'null' is not assignable to type 'string'
let safeName: string | null = null; // ✅ 正确写法

4. 模块解析策略 (moduleResolution)

json
{
  "compilerOptions": {
    "moduleResolution": "bundler"
  }
}

可选值:

选项说明适用场景
Classic经典解析旧版 TS
NodeNode.js 算法Node.js 项目
Bundler打包器解析Vite/Webpack ✅ (推荐)

5. ES 模块互操作 (esModuleInterop)

json
{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

作用: 允许默认导入 CommonJS 模块

typescript
// esModuleInterop: false
import * as express from 'express'; // ❌ 繁琐

// esModuleInterop: true
import express from 'express'; // ✅ 简洁

四、常用配置组合

1. Vue 3 + Vite 项目配置

json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "skipLibCheck": true,
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.vue"],
  "exclude": ["node_modules", "dist"]
}

2. React 项目配置

json
{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "ESNext",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

3. Node.js 项目配置

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "CommonJS",
    "lib": ["ES2022"],
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "**/*.test.ts"]
}

4. 库开发配置

json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "lib": ["ES2020", "DOM"],
    "declaration": true,
    "declarationDir": "./types",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "bundler",
    "resolveJsonModule": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist", "tests"]
}

五、重要配置项速查表

1. 语言环境配置

配置项推荐值说明
targetES2020编译后的 JS 版本
lib["ES2020", "DOM"]引入的 API 类型定义
jsxpreserve/react-jsxJSX 代码处理方式

2. 模块系统配置

配置项推荐值说明
moduleESNext使用的模块系统
moduleResolutionbundler模块解析策略
baseUrl"."解析非相对模块的基准目录
paths{"@/": ["src/"]}路径映射别名

3. 类型安全配置

配置项推荐值说明
stricttrue开启所有严格检查
noImplicitAnytrue禁止隐式 any
strictNullCheckstrue严格 null 检查
noUnusedLocalstrue检查未使用的局部变量
noUnusedParameterstrue检查未使用的参数

4. 输出配置

配置项推荐值说明
outDir./dist输出目录
rootDir./src源文件目录
declarationtrue生成 .d.ts 声明文件
sourceMaptrue生成 source map
removeCommentsfalse是否删除注释

5. 互操作性配置

配置项推荐值说明
esModuleInteroptrueES 模块互操作
allowSyntheticDefaultImportstrue允许默认导入
resolveJsonModuletrue支持导入 JSON

6. 构建优化配置

配置项推荐值说明
skipLibChecktrue跳过声明文件检查
forceConsistentCasingInFileNamestrue强制文件名大小写一致
isolatedModulestrue确保单文件转译
noEmittrue不生成输出文件 (仅类型检查)

六、路径别名配置

1. 基础路径映射

json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@components/*": ["src/components/*"],
      "@utils/*": ["src/utils/*"],
      "@types/*": ["src/types/*"]
    }
  }
}

使用示例:

typescript
// 导入组件
import Button from '@/components/Button'
import { formatDate } from '@utils/date'
import type { User } from '@types/user'

2. 多路径匹配

json
{
  "paths": {
    "react": ["./node_modules/@types/react"],
    "react-dom": ["./node_modules/@types/react-dom"],
    "@/*": ["src/*", "packages/*/src"]
  }
}

七、常见错误与解决方案

1. Cannot find module

错误:

Cannot find module '@/components/Button' or its corresponding type declarations.

解决方案:

json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*"]
}

2. Module has no default export

错误:

Module '"xxx"' has no default export.

解决方案:

json
{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}

3. Implicit any type

错误:

Parameter 'x' implicitly has an 'any' type.

解决方案:

json
{
  "compilerOptions": {
    "noImplicitAny": true
  }
}

并为参数添加类型注解:

typescript
// ❌ 错误
function add(x, y) {
  return x + y;
}

// ✅ 正确
function add(x: number, y: number): number {
  return x + y;
}

八、最佳实践建议

1. ✅ 推荐的配置组合

json
{
  "compilerOptions": {
    // 基础配置
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    
    // 严格类型检查
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    
    // 互操作性
    "esModuleInterop": true,
    "resolveJsonModule": true,
    
    // 构建优化
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

2. ⚠️ 避免的配置陷阱

json
{
  "compilerOptions": {
    // ❌ 不要关闭严格检查
    "strict": false,
    
    // ❌ 不要忽略未使用变量(除非有特殊需求)
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    
    // ❌ 不要使用过时的 target
    "target": "ES3"
  }
}

3. 🎯 根据项目类型选择

项目类型关键配置说明
前端应用module: ESNext, moduleResolution: bundler配合打包器使用
Node.js 后端module: CommonJS, target: ES2022Node.js 原生支持
库开发declaration: true, outDir: dist需要生成类型声明
Monorepocomposite: true, incremental: true项目引用

九、高级特性

1. 项目引用 (Project References)

适用于 Monorepo 项目:

json
{
  "files": [],
  "references": [
    { "path": "./packages/core" },
    { "path": "./packages/utils" }
  ]
}

子项目配置:

json
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "declarationMap": true
  }
}

2. 增量编译

json
{
  "compilerOptions": {
    "incremental": true,
    "tsBuildInfoFile": ".tsbuildinfo"
  }
}

3. 扩展其他配置

json
{
  "extends": "@tsconfig/node20/tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist"
  }
}

十、面试常见问题

Q1: tsconfig.json 的作用是什么?

回答要点:

  1. 指定根文件和环境
  2. 配置编译器选项(target、module 等)
  3. 控制类型检查规则
  4. 管理文件包含/排除规则

Q2: strict 模式包含哪些检查?

回答要点:

  • strictNullChecks(严格 null 检查)
  • strictFunctionTypes(严格函数类型)
  • strictBindCallApply(严格 bind/call/apply)
  • strictPropertyInitialization(属性初始化检查)
  • noImplicitThis(隐式 this 检查)
  • alwaysStrict(严格模式)

Q3: esModuleInterop 和 allowSyntheticDefaultImports 的区别?

回答要点:

  • esModuleInterop: 运行时辅助函数,实现真正的互操作
  • allowSyntheticDefaultImports: 仅类型层面允许默认导入
  • 通常一起使用,esModuleInterop 已包含后者功能

Q4: 如何配置路径别名?

回答要点:

  1. 设置 baseUrl 为根目录
  2. 使用 paths 定义路径映射
  3. 同时配置打包器(Vite/Webpack)的别名

十一、记忆口诀

📝 快速记忆

tsconfig 配置口诀:

  • target 定版本,module 选系统
  • strict 开严格,interop 助导入
  • paths 设别名,include 包文件
  • skipLib 跳检查,declaration 生声明

十二、延伸学习

相关资源

下一步建议

最近更新